package com.xinjie.www.service.user;

import com.xinjie.www.dao.BaseDao;
import com.xinjie.www.dao.user.UserDao;
import com.xinjie.www.dao.user.UserDaoImpl;
import com.xinjie.www.po.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserServiceImpl implements UserService {
    private final UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userName, String password) {
        Connection connection = null;
        User user = null;

        try {
            connection = BaseDao.getConnection();
            System.out.println(connection!=null);
            user = userDao.getLoginUser(connection, userName, password);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            boolean b = BaseDao.closeResource(connection, null, null);
            if (b) {
                System.out.println("关闭成功");
            }
        }
        return user;
    }

    @Override
    public boolean updateUsername(int id, String newUsername, String oldUsername) {
        Connection conn = null;
        boolean flag = false;
        try {
            conn = BaseDao.getConnection();
            if (userDao.updateUsername(conn, id, newUsername, oldUsername) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }

    @Override
    public boolean updatePassword(int id, String password) {
        Connection conn = null;
        boolean flag = false;
        try {
            conn = BaseDao.getConnection();
            if (userDao.updatePassword(conn, id, password) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }

    @Override
    public boolean updateSign(int id, String sign) {
        Connection conn = null;
        boolean flag = false;
        try {
            conn = BaseDao.getConnection();
            if (userDao.updateSign(conn, id, sign) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }

    @Override
    public boolean registerUser(String name, String password, String sex, String birthday, String sign) {
        Connection conn = null;
        boolean flag = false;
        try {
            conn = BaseDao.getConnection();
            if (userDao.registerUser(conn, name, password, sex, birthday, sign) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }

    @Override
    public List<User> getAllUser() {
        Connection conn = null;
        List<User> list = new ArrayList<>();
        try {
            conn = BaseDao.getConnection();
            list = userDao.getAllUser(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return list;
    }

    @Override
    public List<User> getBanUser(int tiebaId) {
        Connection conn = null;
        List<User> users = new ArrayList<>();
        try {
            conn = BaseDao.getConnection();
            users = userDao.getBanUser(conn, tiebaId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return users;
    }
}
